<template>
  <el-dialog
    :title="dialist.isAdd ? '添加商品规格' : '编辑商品规格'"
    :visible.sync="dialist.isShow"
    center
    :before-close="cancle"
  >
    <el-form :model="list" ref="diaform" :rules="rules" >
      <el-form-item label="规格名称" :label-width="formLabelWidth" prop="specsname">
        <el-input v-model="list.specsname" placeholder="请输入规格名称"></el-input>
      </el-form-item>
      <el-form-item label="规格属性" :label-width="formLabelWidth" v-for="(item,index) of attrArr" :key="index">
          <el-input   autocomplete="off" v-model="item.value" style="width:50%;float:left" placeholder="请输入规格属性"></el-input>
          <el-button type="primary" style="float:left;margin-left:10px" @click="addAttr" v-if="index==0">新增规格属性</el-button>
          <el-button type="danger" style="float:left;margin-left:10px" v-else @click="delAttr(index)">删除</el-button>
      </el-form-item>     
      <el-form-item label="状态" :label-width="formLabelWidth">
        <el-switch
          v-model="list.status"
          :active-value="1"
          :inactive-value="2"
        ></el-switch>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="cancle">取 消</el-button>
      <el-button type="primary" v-if="dialist.isAdd" @click="add"
        >添 加</el-button
      >
      <el-button type="primary" v-else @click="edit">编 辑</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { addSpecs,editSpecs, getSpecsOne } from "../../util/axios";

// import{mapActions} from "vuex"
export default {
  data() {
    return {
      list: {
        specsname: "",
        attrs: "",
        status: 1,
      },
      //用来循环添加输入框
      attrArr:[{value:""}],
      formLabelWidth: "120px",
      defaultProps: {
        children: "children",
        label: "title",
      },
      rules: {
        specsname: [
          { required: true, message: "请输入管理员名称", trigger: "blur" },
          { min: 2, max: 8, message: "长度在 2 到 8 个字符", trigger: "blur" },
        ],
      },
    };
  },
  props: ["dialist"],
  methods: {
    addAttr(){
      if(this.attrArr.length <5){
          this.attrArr.push({value:""})
      }else{
        this.$message.info("数量达到最大,无法再增加");

      }
        
    },
    delAttr(n){
       this.attrArr.splice(n,1)
    },
    //点击取消后隐藏dialog
    cancle() {
      //隐藏dialog
      this.$emit("cancle", false);
      //清空表格
      this.list = {
        specsname: "",
        attrs: "",
        status: 1,
      };
      //清空验证
      this.$refs.diaform.resetFields();
      this.attrArr=[{value:""}];
    },
    //添加功能
    add() {
      this.$refs.diaform.validate((val) => {
        if (val) {
          let newArr = [];
          this.attrArr.forEach(item=>{
            newArr.push(item.value);
          });
          this.list.attrs = newArr.join(",");   
          addSpecs(this.list)
            .then((res) => {
              if (res.code == 200) {
                this.$message.success(res.msg);
                this.$emit("rolad");
                this.cancle();
              } else {
                this.$message.error(res.msg);
              }
            })
            .catch((err) => {
              this.$message.error(err.msg);
            });
        }
      });
    },
    //dialog的编辑
    edit() {
      //  console.log(this.list);
      this.$refs.diaform.validate((val) => {
        if (val) {
          let newArr = [];
          this.attrArr.forEach(item=>{
            newArr.push(item.value);
          });
          this.list.attrs = newArr.join(",");
          editSpecs(this.list)
            .then((res) => {
              if (res.code == 200) {
                this.$message.success(res.msg);
                this.$emit("rolad")
                 this.cancle();
              } else {
                this.$message.error(res.msg);
              }
            })
            .catch((err) => {
              this.$message.error(err.msg);
            });
        }
      });
    },
    lookup(id) {
      getSpecsOne({ id }).then((res) => {
        if (res.code == 200) {
         this.list = res.list[0];
         this.attrArr=[];
         this.list.attrs.map(item=>{
          this.attrArr.push({"value":item})
         })
          this.list.id = id;
        }
      });
    },
  },
};
</script>

<style lang="stylus" scoped></style>